package com.erp.ae.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.ae.domain.AeMsgSave;
import com.erp.ae.domain.bo.AeMsgSaveBo;
import com.erp.ae.domain.vo.AeMsgSaveVo;
import com.erp.ae.mapper.AeMsgSaveMapper;
import com.erp.ae.service.IAeMsgSaveService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ae消息推送信息保存Service业务层处理
 *
 * @author erp
 * @date 2023-07-26
 */
@RequiredArgsConstructor
@Service
public class AeMsgSaveServiceImpl implements IAeMsgSaveService {

    private final AeMsgSaveMapper baseMapper;

    /**
     * 查询ae消息推送信息保存
     */
    @Override
    public AeMsgSaveVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ae消息推送信息保存列表
     */
    @Override
    public TableDataInfo<AeMsgSaveVo> queryPageList(AeMsgSaveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AeMsgSave> lqw = buildQueryWrapper(bo);
        Page<AeMsgSaveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ae消息推送信息保存列表
     */
    @Override
    public List<AeMsgSaveVo> queryList(AeMsgSaveBo bo) {
        LambdaQueryWrapper<AeMsgSave> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AeMsgSave> buildQueryWrapper(AeMsgSaveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AeMsgSave> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSellerId()), AeMsgSave::getSellerId, bo.getSellerId());
        lqw.eq(StringUtils.isNotBlank(bo.getMessageType()), AeMsgSave::getMessageType, bo.getMessageType());
        lqw.eq(StringUtils.isNotBlank(bo.getAeMsg()), AeMsgSave::getAeMsg, bo.getAeMsg());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), AeMsgSave::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增ae消息推送信息保存
     */
    @Override
    public Boolean insertByBo(AeMsgSaveBo bo) {
        AeMsgSave add = BeanUtil.toBean(bo, AeMsgSave.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ae消息推送信息保存
     */
    @Override
    public Boolean updateByBo(AeMsgSaveBo bo) {
        AeMsgSave update = BeanUtil.toBean(bo, AeMsgSave.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AeMsgSave entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ae消息推送信息保存
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
